package com.wangzeqi.neihanduanzi.news.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.news.base.BaseCombination;

/**
 * Created by dllo on 16/1/26.
 */
public class CombinatonPagerAdapter extends PagerAdapter {
    private RequestQueue queue;
    private BaseCombination base;
    private Context context;

    public CombinatonPagerAdapter(BaseCombination base, Context context) {
        queue = Volley.newRequestQueue(context);
        this.base = base;
        this.context = context;
    }




    @Override
    public int getCount() {
        return base != null && base.getPhotos().size() > 0 ? base.getPhotos().size() : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
    public void getImage(NetworkImageView imageView, String url) {
        final ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
            }
        });
        imageView.setImageUrl(url, imageLoader);

    }
    public ImageLoader getImage( String url) {
        final ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return getBitmap(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
            }
        });
        return imageLoader;


    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.combination_pager,null);
        NetworkImageView imageView = (NetworkImageView) view.findViewById(R.id.combination_img);
        getImage(imageView,base.getPhotos().get(position).getImgurl());
        container.addView(view);
        return view;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }
}
